package com.ly.mpayment.persist.service.upproductlog;

import java.io.Serializable;
import java.util.Map;

import com.ly.mpayment.persist.entity.UpShopProduct;
import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.UpProductLog;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
@Service
public class UpProductLogServiceImpl extends AbstractService<UpProductLog, Serializable> implements IUpProductLogService{

	@Override
	public boolean addUpproductlog(UpProductLog log) {
		if(log != null){
			this.saveOrUpdate(log);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateUpproductlog(UpProductLog log) {
		if(log != null){
			this.saveOrUpdate(log);
			return true;
		}
		return false;
	}

	@Override
	public UpProductLog findById(String id) {
		return this.findUnique("select u from UpProductLog u where u.uploguuid = ?", id);
	}

	@Override
	public Page<UpProductLog> findPageUpproductlog(Map<String, Object> parameters, int start, int pageSize) {

		String hql = "select u from UpProductLog u left join fetch u.upShopPro up left join fetch up.user left join fetch u.userLog  where  1 = 1";
		if(parameters.containsKey(":starttime")){
			hql += "and DATE_FORMAT(u.createtime,'%Y-%m-%d') >= '"+parameters.get(":starttime") +"' ";
			parameters.remove(":starttime");
		}

		if(parameters.containsKey(":endtime")){
			hql += "and DATE_FORMAT(u.createtime,'%Y-%m-%d') <= '"+parameters.get(":endtime") +"' ";
			parameters.remove(":endtime");
		}
		hql += " #[and u.upShopPro.user.useruuid = :useruuid]# order by u.createtime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public void deleteLog(UpShopProduct product) {
		this.getSession().createSQLQuery("delete from upproductlog where upprouuid = '"+product.getUpprouuid()+"'").executeUpdate();
	}
}
